package com.crean.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crean.core.api.vo.ResultVo;
import com.crean.demo.controller.vo.UserAddVo;
import com.crean.demo.controller.vo.UserVo;
import com.crean.demo.entity.User;
import com.crean.demo.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 用户相关接口
 *
 * @author lfr
 */
@Api(tags = "用户接口")
@RestController
@RequestMapping("/v1/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @ApiOperation(value = "分页获取用户列表", notes = "分页获取用户列表")
    @GetMapping(value = "/page")
    public ResultVo<IPage<UserVo>> page(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        IPage<User> page = userService.page(new Page(pageNo, pageSize), null);
        IPage<UserVo> result = new Page<>();
        BeanUtils.copyProperties(page, result);
        return ResultVo.ok(result);
    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "用户添加", notes = "用户添加")
    public ResultVo<?> add(@Valid @RequestBody UserAddVo vo) {
        User entity = new User();
        BeanUtils.copyProperties(vo, entity);
        userService.save(entity);
        return ResultVo.ok("添加成功！");
    }
}
